import 'package:x3_base_core/base_utils/base_utils.dart';
import 'package:x3_base_core/base_models/base_http_result_model.dart';
import 'package:x3_base_core/base_models/base_http_request_model.dart';
import 'package:x3_base_core/base_utils/base_form_transform_util.dart';

class BaseCatalogRequestModel {

  /// 构建组件数据
  List<BaseHttpRequestModel> request;

  /// 渲染组件源数据
  Map<String, dynamic> dataSource;

  BaseCatalogRequestModel(BaseHttpResultModel result) {
    var data = result.data;
    if (result.success == true) {
      this.request = data.containsKey('request') ? BaseFormTransformUtil.transformToListRequestData(data['request']) : <BaseHttpRequestModel>[];
      this.dataSource = data.containsKey('dataSource') ? data['dataSource'] : {};
    } else {
      this.request = <BaseHttpRequestModel>[];
      this.dataSource = {};
      BaseUtils.showToast('网络请求异常');
    }
  }

  /// 获取 DataSource 数据
  dynamic findDataSource(String fieldName) {
    var data = this.dataSource.containsKey(fieldName) ? this.dataSource[fieldName] : null;
    if (data is List || data is String) {
      data = data.length == 0 ? null : data;
    }
    if (data is Map) {
      data = data.keys.length == 0 ? null : data;
    }
    return data;
  }

  @override
  String toString() {
    return 'BaseCatalogRequestModel{request: $request, dataSource: $dataSource}';
  }

}
